package com.ncloud.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.exception.UserException;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by shiqiang on 2017/7/4.
 */
public class JsonUtils {
    public static JSONObject replaceValue(JSONObject json, String key, Object value, Object newValue){
        JSONObject result = new JSONObject();
        for(Map.Entry<String,Object> entry:json.entrySet()){

            Object obj = entry.getValue();
            result.put(entry.getKey(),obj);
            if(obj instanceof JSONArray){
                replaceValue((JSONArray) obj,key,value,newValue);
            }else if(obj instanceof JSONObject){
                replaceValue((JSONObject)obj,key,value,newValue);
            }else{
                json.replace(key,value,newValue);
            }
        }
        return result;
    }

    public static void replaceValue(JSONArray array,String key,Object value,Object newValue){
        if(array==null) return;
        for(int i=0;i<array.size();i++){
            Object obj = array.get(i);
            if(obj instanceof JSONArray){
                replaceValue((JSONArray) obj,key,value,newValue);
            }else if(obj instanceof JSONObject){
                replaceValue((JSONObject)obj,key,value,newValue);
            }else{

            }
        }
    }

    public static JSONObject addValue(JSONObject json, String key,Object value, Object newValue){
        final String newKey = key+"Name";
        JSONObject result = JSON.parseObject(json.toJSONString());
        for(Map.Entry<String,Object> entry:json.entrySet()){

            Object obj = entry.getValue();
           // result.put(entry.getKey(),obj);
            if(obj instanceof JSONArray){
                result.put(entry.getKey(),addValue((JSONArray) obj,key,value,newValue));
            }else if(obj instanceof JSONObject){
                result.put(entry.getKey(),addValue((JSONObject)obj,key,value,newValue));
            }else{
                if(json.containsKey(key) && value.equals(json.getString(key))){
                    result.put(newKey,newValue);
                }
            }
        }
        return result;
    }

    public static JSONArray addValue(JSONArray array,String key,Object value,Object newValue){
        JSONArray array1 = new JSONArray();
        if(array==null) return null;
        for(int i=0;i<array.size();i++){
            Object obj = array.get(i);
            //array1.add(obj);
            if(obj instanceof JSONArray){
                array1.add(addValue((JSONArray) obj,key,value,newValue));
            }else if(obj instanceof JSONObject){
                array1.add(addValue((JSONObject)obj,key,value,newValue));
            }else{
                array1.add(obj);
            }
        }
        return array1;
    }

    /*解析字符串参数，安装模板生成信息*/
    public static String getTemplateInfo(String srcStr, JSONObject data){
        final String head ="${";
        final String fooled = "}";
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            Object obj = entry.getValue();
            String value ="'";
            StringBuffer sb = new StringBuffer(head);
            sb.append(entry.getKey()).append(fooled);
            if(obj instanceof String){
                value +=obj.toString()+"'";
                if(srcStr.contains(sb.toString())){
                    srcStr = srcStr.replace(sb.toString(),value.toString());
                }
            }else{
                if(srcStr.contains(sb.toString())){
                    srcStr = srcStr.replace(sb.toString(),entry.getValue().toString());
                }
            }
        }
        return srcStr;
    }

    public static String geneTemplateInfo(String srcStr, JSONObject data){
        final String head ="${";
        final String fooled = "}";
        Pattern p = Pattern.compile("\\$\\{(\\w*)\\}");
        // 获取 matcher 对象
        Matcher m = p.matcher(srcStr);
        while(m.find()) {
            //System.out.println(m.group());
            Matcher m2 =  Pattern.compile("(\\w+)").matcher(m.group());
            String key = m2.find()?m2.group():"";
            if(data.containsKey(key)){
                Object obj = data.get(key);
                String value ="'";
                StringBuffer sb = new StringBuffer(head);
                sb.append(key).append(fooled);
                if(obj instanceof String){
                    value +=obj.toString()+"'";
                    if(srcStr.contains(sb.toString())){
                        srcStr = srcStr.replace(sb.toString(),value.toString());
                    }
                }else{
                    if(srcStr.contains(sb.toString())){
                        srcStr = srcStr.replace(sb.toString(),obj.toString());
                    }
                }
            }else{
                throw new UserException(8888,"json参数中不存在key="+key);
            }
        }
        return srcStr;
    }
}
